<template>
  <div>
    <el-row>
      <el-col
        v-for="(item,index) in filterList"
        :key="index"
        :span="4"
      >
        <template v-if="item.formType==='0'">
          <selectTree
            :label="item.label"
            :type="item.type"
            :param-type="'0002'"
            style="width:90%"
            :common-index="index"
            :show-label="item.showLabel"
            :item-url="item.url"
            :placeholder="item.placeholder"
            :common-values="item.value"
            @changeData="changeData"
            @clearTreeSelect="clearTreeSelect"
          />
        </template>
        <template v-if="item.formType==='1'">
          <el-input v-model="item.value" clearable style="width:90%" :placeholder="item.placeholder" />
        </template>
        <template v-if="item.formType==='2'">
          <el-select v-model="item.value" style="width:90%" filterable clearable :placeholder="item.placeholder" @focus="getSelectFouce(item.url,item.placeholder,index,item.type)">
            <el-option
              v-for="(item1,index1) in item.selectOption"
              :key="index1"
              :label="item1[item.showLabel]"
              :value="item1[item.showValue]"
            />
          </el-select>
        </template>
        <template v-if="item.formType==='3'">
          <el-date-picker
            v-model="item.value"
            :type="item.pickerType"
            style="width:90%"
            :value-format="item.pickerFormat"
            :placeholder="item.placeholder"
          />
        </template>
      </el-col>
      <el-col :span="6" style="text-align: left;">
        <div class="grid-content bg-purple-light">
          <el-button type="text" @click="onSubmit">
            搜索
          </el-button>
          <el-button
            type="text"
            @click="reset()"
          >
            重置
          </el-button>
          <el-button type="text" @click="downFile">
            下载
          </el-button>
          <el-button type="text" @click="createProject('create')">
            新增
          </el-button>

        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { Message } from 'element-ui';
import selectTree from '@/components/SelectTree/index.vue';
import { getCommonDataSource } from '@/api/reportCenter';
import { exportExcel } from '@/api/IncomeStatement';
import { downloadFile } from '@/utils/commonMethods.js';
export default {
  components: {
    selectTree
  },
  data() {
    return {
      selectOption: [],
      // 存放选择的条件
      filterList: [
        // 0 是下拉树 1 是input 2 是下拉框 3 是年度日期选择
        {
          label: '大类',
          url: 'option/findByType',
          placeholder: '请选择大类',
          formType: '0',
          value: '',
          symbol: 'eq',
          showLabel: 'text',
          isMust: true,
          type: '0003',
          col: 'PRIOR_TYPE'
        },
        {
          label: '年度',
          placeholder: '年度',
          formType: '3',
          value: '',
          symbol: 'eq',
          pickerType: 'year',
          pickerFormat: 'yyyy',
          isMust: true,
          col: 'YEAR'
        },
        {
          label: '类型',
          url: 'option/findByType',
          placeholder: '请选择类型',
          formType: '2',
          value: '',
          symbol: 'eq',
          showLabel: 'text',
          showValue: 'name',
          isMust: true,
          type: '0004',
          selectOption: [],
          col: 'TYPE'
        },
        {
          label: '资金来源',
          url: 'option/findByType',
          placeholder: '请选择资金来源',
          formType: '2',
          value: '',
          symbol: 'eq',
          showLabel: 'text',
          showValue: 'name',
          isMust: true,
          type: '0007',
          selectOption: [],
          col: 'RESOURCES'
        }

      ]
    };
  },
  methods: {
    getSelectFouce(url, placeholder, index, type) {
      getCommonDataSource({
        type: type
      }, url).then(res => {
        if (res.success) {
          this.filterList[index].selectOption = res.data;
        } else {
          this.filterList[index].selectOption = [];
        }
      });
    },
    clearTreeSelect(row) {
      this.filterList[row.commonIndex].value = '';
    },
    changeData(row) {
      this.filterList[row.commonIndex].value = row.data[row.showLabel];
    },
    createProject(type) {
      const isLowest = this.$parent.$refs.enterpriseTree.isLowest;
      this.$parent.$refs.enterpriseDialog.filterList.map(o => {
        if (o.label === '单位') {
          o.value = this.$parent.$refs.enterpriseTree.enterpriseName;
        }
      });
      if (!isLowest) {
        Message({
          message: '请先选择底层单位',
          type: 'warning'
        });
      } else {
        this.$parent.$refs.enterpriseDialog.dialogVisible = true;
        this.$parent.$refs.enterpriseDialog.isDisable = false;
        this.$parent.$refs.enterpriseDialog.title = '新增';
      }
    },
    downFile() {
      const enterpriseGuid = this.$parent.$refs.enterpriseTree.enterpriseGuid;
      if (enterpriseGuid === '') {
        Message({
          message: '请先选择单位',
          type: 'warning'
        });
      } else if (this.$parent.count === 0) {
        Message({
          message: '数据为空,无法下载',
          type: 'warning'
        });
      } else {
        let filterList1 = [];
        if (enterpriseGuid === 'all') {
          filterList1 = [];
        } else {
          filterList1 = [{
            value: enterpriseGuid,
            col: 'ENTERPRISEGUID',
            symbol: 'in'
          }];
        }
        this.filterList.map(o => {
          filterList1.push({
            value: o.formType !== '3' ? o.value !== '' ? o.value.split(' ')[0] : o.value : o.value,
            col: o.col,
            symbol: o.symbol
          });
        });
        exportExcel({
          columnList: this.$parent.columnList,
          filterList: filterList1,
          headerList: this.$parent.headerList,
          moneyType: '1',
          year: localStorage.getItem('chooseYear'),
          month: '',
          orderBy: '',
          pageType: '1',
          pageSize: this.$parent.pageSize,
          pageIndex: this.$parent.pageIndex
        }).then(res => {
          downloadFile(res, `收入表.xls`, '.xls');
        });
      }
    },
    reset() {
      this.filterList.map(o => {
        o.value = '';
      });
      if (this.$parent.$refs.enterpriseTree.enterpriseGuid !== '') {
        this.onSubmit();
      } else {
        Message({
          message: '重置成功',
          type: 'success'
        });
      }
    },
    onSubmit() {
      this.$parent.pageSize = 50;
      this.$parent.pageIndex = 1;
      this.$parent.getData();
    }
  }
};
</script>
<style lang="scss" scoped>
.buttonClass {
  width: 80px;
  display: inline-block;
  // background: rgba(51, 119, 255, 1);
  border-radius: 4px;
}
</style>

